package cn.com.godliu.gateway.aes;

import cn.com.godliu.gateway.aes.ByteFormat;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * @author: Chenzhenyong
 * @description: AES工具类
 * @date: Created in 15:18 2018/7/25
 */
public class AESUtil {

    public static final String KEY = "e1c3d0de067d4666";
    private static byte[] iv = "0000000000000000".getBytes();

    /**
     * 解密AES加密过的字符串
     *
     * @param content  AES加密过过的内容
     * @param password 加密时的密码
     * @return 明文
     */
    public static String decrypt(String content, String password) {
        try {
            IvParameterSpec ivspec = new IvParameterSpec(iv);
            SecretKeySpec secretKey = new SecretKeySpec(password.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, secretKey, ivspec);
            byte[] contentByte = ByteFormat.hexToBytes(content);
            byte[] result = cipher.doFinal(contentByte);
            return new String(result);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 加密
    public static String encrypt(String content, String sKey) throws Exception {
        try {
            IvParameterSpec ivspec = new IvParameterSpec(iv);
            SecretKeySpec secretKey = new SecretKeySpec(sKey.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);
            byte[] result = cipher.doFinal(content.getBytes("UTF-8"));
            return ByteFormat.bytesToHexString(result);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static void main(String[] args) throws Exception {
        String aesKey = "e1c3d0de067d4666";
//		System.out.println(decrypt("799AF1D3B9C71959151EF84CD501DF6EA8381FDB2188ACA8687A05F062F2EC493C2E28EBD56FE779F71B9D749DDB3C3B65066666527C2C158E97E3BFA95A7613B484A87C885EC26E435A6F10414C633B9836F49AE97D16ABC935EC7C6598F9D3", aesKey));


//		String test = "123Abc@#{我是谁}path/test/ppp";
//		String encryptstr = encrypt(test,aesKey);
//		System.out.println("加密:"+encryptstr);
//		System.out.println("解密:"+decrypt(encryptstr,aesKey));


        String body = "";
        System.out.println("解密body:" + decrypt(body, aesKey));
    }
}


